package co.fitstart.admin.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dynamic;
import co.fitstart.admin.dao.UserMonthCountDao;
import co.fitstart.admin.handler.SmsSender;
import co.fitstart.admin.service.UserMonthCountService;
import co.fitstart.entity.user.UserMonthCount;

/**
 * 
 * @author LZJ
 *
 */
@Service("userMonthCountService")
public class UserMonthCountServiceImpl implements UserMonthCountService{
	
	private static final Logger logger = LoggerFactory.getLogger(UserMonthCountServiceImpl.class);
    
    @Dynamic
    private UserMonthCountDao userMonthCountDao;  
    
    @Autowired
    private SmsSender smsSender;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
	Date date = new Date();
	String thisMonth = sdf.format(date.getTime());
	
	
	public void minusExpireTimes(String thisMonth, Long StudentId,String mobile,String scheduleTime) {
		
		logger.info("thisMonth [{}] StudentId [{}] mobile [{}] scheduleTime [{}]",thisMonth,  StudentId, mobile, scheduleTime);
		
		UserMonthCount userMonthCount = userMonthCountDao.getExpireTimes(thisMonth, StudentId);
		
		if(userMonthCount != null && userMonthCount.getExpireTimes() < 3) {
			
//			userMonthCount.setExpireTimes((userMonthCount.getExpireTimes()-1);
			genericDaoSupport.update(userMonthCount);		
					
		}
		
		if(userMonthCount != null && userMonthCount.getExpireTimes()>3) {				
			
			smsSender.sendOverSchedule(mobile,scheduleTime);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see co.fitstart.admin.service.UserMonthCountService#getAllTimes(java.lang.Long, java.lang.String)
	 */
	public UserMonthCount getAllTimes(Long studentId, String thisMonth) {
		
		
		return userMonthCountDao.loadAllTimes(thisMonth, studentId);
	}

	
	
	
	
}
